package bman.tools;

/**
 * Performance test for LogBuffer. Run the main() method of this class to run the test.
 * This is not a JUnit test.
 * @author MrJacky
 *
 */
public class LogBufferTest extends Thread
{
	static LogBuffer lb = new LogBuffer();
	public long time = 0;
	
	public void run()
	{
		System.out.println(this.getId()+ " running...");
		time = LogBufferTest.load100MB();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		int testRuns = 10;
		
		/*
		 * Single Thread Test
		 */
		long total = 0;
		for(int i=0;i< testRuns; i++)
		{
			total += load100MB();
		}
		float ave = total/testRuns;

		System.out.println("-----------------------------");
		System.out.println("Test runs: "+testRuns);
		System.out.println("Ave. time: "+ave);
		System.out.println("-----------------------------");
		
		/*
		 * Multi-Thread Tests
		 */
		LogBufferTest[] t = new LogBufferTest[testRuns];
		for(int i=0;i< testRuns; i++)
		{
			t[i] = new LogBufferTest();
			System.out.println("Starting thread "+i);
			t[i].start();
		}
		long total2 = 0;
		for(int i=0;i<testRuns;i++)
		{
			t[i].join();
			total2 += t[i].time;
		}
		float multiThreadAve = total2/testRuns;
		System.out.println("-----------------------------");
		System.out.println("Threads  : "+testRuns);
		System.out.println("Ave. time: "+multiThreadAve);
		System.out.println("-----------------------------");
		
		JFile.write("2MB_sample.log",lb.getContents());
	}
	
	/**
	 * Loads a 100MB of data into the buffer
	 */
	public static long load100MB()
	{
		//100MB worth of sample data
		String sample = "1234567890";
		int max = 100000000/sample.length();

		long start = System.currentTimeMillis();
		for(int i=0; i<max;i++)
		{
			lb.log(sample);
		}
		long total = System.currentTimeMillis() - start;
		System.out.println("Total time to fill buffer: "+total);
		return total;
	}

}
